#include "ForgeRoom.hh"
#include <iostream>
#include <random>

ForgeRoom::ForgeRoom(const std::string& name, const std::string& text)
    : Room(name, text) {}

void ForgeRoom::enterRoom(Player& player) {
    std::cout << "\nYou have entered the Forge Room: " << getName() << "\n";
    std::cout << "Here you can upgrade your weapon or armor.\n";

    while (true) {
        player.displayStatus();
        std::cout << "\n[1] Upgrade Weapon\n[2] Upgrade Armor\n[3] Leave\nChoose: ";
        int choice=IO::getNumber();
        //声明一个指向item对象的指针target，后面根据玩家选择把它指向对应装备方便操作
        //Declare a pointer to the item object, and then point it to the corresponding equipment according to the player's choice
        Item* target = nullptr;
        //把原先的属性值保存，在强化失败后可用其来回复
        //Save the original attribute value and use it to restore it if the enhancement fails
        unsigned originalValue = 0; 

        if (choice == 1) {
            target = player.getEquippedWeapon();
            if (!target) {
                std::cout << "You don't have a weapon equipped.\n";
                continue;
            }
            originalValue = target->value;
        } else if (choice == 2) {
            target = player.getEquippedArmor();
            if (!target) {
                std::cout << "You don't have armor equipped.\n";
                continue;
            }
            originalValue = target->value;
        } else if (choice == 3) {
            std::cout << "You have left the Forge Room.\n";
            break;
        } else {
            std::cout << "Invalid input.\n";
            continue;
        }

        if (target->type != Item::Type::Weapon && target->type != Item::Type::Armor) {
            std::cout << "Only weapons and armors can be upgraded.\n";
            continue;
        }

        if (target->level >= 3) {
            std::cout << "Item is already at max level.\n";
            continue;
        }

        int cost = 0, chance = 100;
        if (target->level == 0) { cost = 20; chance = 100; }
        else if (target->level == 1) { cost = 40; chance = 50; }
        else if (target->level == 2) { cost = 60; chance = 30; }

        if (player.getGold() < cost) {
            std::cout << "Not enough gold to upgrade. Required: " << cost << "\n";
            continue;
        }

        player.spendGold(cost);

        static std::default_random_engine rng(std::random_device{}()); //创建静态随机数引擎 //Create a static random number engine
        std::uniform_int_distribution<> dist(1, 100); //定义一个均匀整数分布对象 dist，范围是闭区间 [1, 100] //Define a uniform integer distribution object dist with a range of closed intervals [1, 100]
        int roll = dist(rng); //把生成的随机数储存在roll里 //Store the generated random number in the roll
        //升级逻辑
        //Escalation logic
        if (roll <= chance) {
            target->level++;
            int increase = 0;
            if (target->level == 1) increase = 2; //根据等级给增益 //Buffs are given based on level
            else if (target->level == 2) increase = 3;
            else if (target->level == 3) increase = 5;

            target->value += increase;

            if (target->type == Item::Type::Weapon) {
                player.setAttack(player.getAttack() + increase);
            } else if (target->type == Item::Type::Armor) {
                player.setDefense(player.getDefense() + increase);
            }

            std::cout << "Upgrade successful! New level: " << target->level << "\n";
        } else {
            //降低能力值根据现在的等级
            //Reduces stats based on current level
            int lost = 0;
            if (target->level == 0) lost = 0;
            else if (target->level == 1) lost = 2;
            else if (target->level == 2) lost = 2 + 3;
            else if (target->level == 3) lost = 2 + 3 + 5;

            target->value -= lost;
            if (target->type == Item::Type::Weapon) {
                player.setAttack(player.getAttack() - lost);
            } else if (target->type == Item::Type::Armor) {
                player.setDefense(player.getDefense() - lost);
            }

            target->level = 0;
            std::cout << "Upgrade failed! Item level reset to 0.\n";
        }
    }
}
